<?php

use App\Models\System\Admin;
use App\Models\System\Attachment;
use App\Models\System\Config;
use App\Models\System\Dict;
use App\Models\System\DictItem;
use App\Models\System\Menu;
use App\Models\System\Role;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create(Admin::getTableName(), function (Blueprint $table) {
            $table->id();

            $table->string('username')
                ->unique()
                ->comment('账号');

            $table->string('password')
                ->nullable()
                ->comment('密码');

            $table->string('avatar')
                ->nullable()
                ->comment('头像');

            $table->string('nickname')
                ->nullable()
                ->comment('昵称');

            $table->enum('status', \App\Enum\Models\System\Admin\Status::values())
                ->default(\App\Enum\Models\System\Admin\Status::Normal)
                ->comment('状态');

            $table->timestamps();

            $table->comment('管理员表');
        });

        Schema::create(Menu::getTableName(), function (Blueprint $table) {
            $table->id();

            $table->string('path')
                ->nullable()
                ->comment('路径');

            $table->string('title')
                ->comment('标题');

            $table->string('name')
                ->unique()
                ->comment('命名');

            $table->string('code')
                ->default('')
                ->comment('权限标识');

            $table->string('component')
                ->nullable()
                ->comment('组件');

            $table->enum('type', \App\Enum\Models\System\Menu\Type::values())
                ->default(\App\Enum\Models\System\Menu\Type::Catalog)
                ->comment('类型');

            $table->enum('status', \App\Enum\Models\System\Menu\Status::values())
                ->default(\App\Enum\Models\System\Menu\Status::Normal)
                ->comment('状态');

            $table->unsignedInteger('p_id')
                ->default(0)
                ->comment('上级');

            $table->unsignedTinyInteger('sort')
                ->default(0)
                ->comment('排序');

            $table->string('meta', 1000)
                ->default('')
                ->comment('meta数据(json)');

            $table->timestamps();

            $table->index(['code']);

            $table->comment('菜单表');
        });

        Schema::create(Role::getTableName(), function (Blueprint $table) {
            $table->id();

            $table->string('title')
                ->comment('名称');

            $table->string('code')
                ->unique()
                ->comment('角色标识');

            $table->unsignedTinyInteger('sort')
                ->default(0)
                ->comment('排序');

            $table->enum('status', \App\Enum\Models\System\Role\Status::values())
                ->default(\App\Enum\Models\System\Role\Status::Normal)
                ->comment('状态:normal=正常,disabled=禁用');

            $table->timestamps();

            $table->index(['code']);

            $table->comment('角色表');
        });

        Schema::create('menu_role', function (Blueprint $table) {
            $table->integer('role_id')
                ->comment('角色');

            $table->integer('menu_id')
                ->comment('菜单标识');

            $table->timestamps();

            $table->unique(['role_id', 'menu_id']);

            $table->comment('权限角色关系表');
        });

        Schema::create('admin_role', function (Blueprint $table) {
            $table->integer('admin_id')
                ->comment('管理员');

            $table->integer('role_id')
                ->comment('角色');

            $table->timestamps();

            $table->unique(['role_id', 'admin_id']);

            $table->comment('管理员角色关系表');
        });

        Schema::create(Dict::getTableName(), function (Blueprint $table) {
            $table->id();

            $table->string('title')
                ->comment('字典名称');

            $table->string('code')
                ->comment('唯一编码');

            $table->timestamps();

            $table->unique(['code']);

            $table->comment('字典表');
        });

        Schema::create(DictItem::getTableName(), function (Blueprint $table) {
            $table->id();

            $table->integer('dict_id')
                ->default(0)
                ->comment('所属字典');

            $table->string('title')
                ->default('')
                ->comment('标签');

            $table->string('value')
                ->default('')
                ->comment('值');

            $table->integer('sort')
                ->default(0)
                ->comment('排序');

            $table->enum('status', \App\Enum\Models\System\DictItem\Status::values())
                ->default(\App\Enum\Models\System\DictItem\Status::Normal)
                ->comment('状态');

            $table->timestamps();

            $table->unique(['value', 'dict_id']);

            $table->comment('字典项表');
        });

        Schema::create(Attachment::getTableName(), function (Blueprint $table) {
            $table->id();

            $table->integer('admin_id')
                ->default(0)
                ->comment('上传管理员');

            $table->integer('user_id')
                ->default(0)
                ->comment('上传用户');

            $table->string('filename')
                ->default('')
                ->comment('文件名');

            $table->string('url')
                ->default('')
                ->comment('地址(不带域名)');

            $table->string('url_md5')
                ->comment('地址的md5 用于知道文件地址 进行数据库查找');

            $table->string('fileinfo')
                ->default('')
                ->comment('文件信息');

            $table->string('mimetype')
                ->default('')
                ->comment('文件类型');

            $table->integer('filesize')
                ->default(0)
                ->comment('文件大小');

            $table->string('extparam')
                ->default('')
                ->comment('透传数据');

            $table->string('sha1')
                ->default('')
                ->comment('sha1');

            $table->timestamps();

            $table->unique(['url_md5']);

            $table->unique(['sha1']);

            $table->comment('附件表');
        });

        Schema::create(Config::getTableName(), function (Blueprint $table) {
            $table->id();

            $table->string('title')
                ->default('')
                ->comment('名称');

            $table->string('tip')
                ->default('')
                ->comment('提示');

            $table->string('name')
                ->default('')
                ->comment('命名');

            $table->text('value')
                ->nullable()
                ->comment('值');

            $table->string('group')
                ->default('')
                ->comment('分组');

            $table->string('type')
                ->default('')
                ->comment('类型');

            $table->tinyInteger('is_public')
                ->default(0)
                ->comment('是否公开(接口响应增加)');

            $table->string('remark')
                ->default('')
                ->comment('备注(开发人员查看)');

            $table->string('rules')
                ->default('')
                ->comment('验证规则(,)');

            $table->text('prop')
                ->nullable()
                ->comment('属性(json)');

            $table->integer('sort')
                ->default(0)
                ->comment('排序');

            $table->unique(['name']);

            $table->comment('配置表');

            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists(Admin::getTableName());
        Schema::dropIfExists(Menu::getTableName());
        Schema::dropIfExists(Role::getTableName());
        Schema::dropIfExists('menu_role');
        Schema::dropIfExists('admin_role');
        Schema::dropIfExists(Dict::getTableName());
        Schema::dropIfExists(DictItem::getTableName());
        Schema::dropIfExists(Attachment::getTableName());
        Schema::dropIfExists(Config::getTableName());
    }
};
